package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.CodeEnum;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.member.ao.SociUser;
import com.atguigu.gulimall.member.exception.PhoneExist;
import com.atguigu.gulimall.member.exception.UserNameExist;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.feign.OrderFeignService;
import com.atguigu.gulimall.member.vo.MemberRegisterVo;
import com.atguigu.gulimall.member.vo.UserLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author dcj
 * @email dcj@gmail.com
 * @date 2023-10-31 20:49:51
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @Autowired
    private OrderFeignService orderFeignService;


    @GetMapping("/test21/{orderSn}")
    public R test21(@PathVariable("orderSn") String orderSn){
        return orderFeignService.getOrderStatus(orderSn);
    }


    @RequestMapping("/authLogin")
    public R authLogin(@RequestBody SociUser vo){
        MemberEntity memberEntity = memberService.AuthLogin(vo);
        if (memberEntity !=null){
            return R.ok().setData(memberEntity);
        }else {
            return R.error(CodeEnum.LOGINACC_OR_PWD_ERROR);
        }

    }
    @RequestMapping("/login")
    public R login(UserLoginVo vo){
        memberService.login(vo);
        return R.ok();
    }

    @RequestMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo){
        try {
            memberService.registerMember(vo);
        }catch (UserNameExist | PhoneExist e){
            return R.error(CodeEnum.LOGINACC_OR_PWD_ERROR);
        }
        return R.ok();
    }

    @RequestMapping("/coupons")
    public R test(){
        MemberEntity member=new MemberEntity();
        member.setNickname("张三");

        R memberCoupons = couponFeignService.memberCoupons();
        return R.ok().put("member",member).put("coupon",memberCoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
